#引用了几个兼容性强一点的模块（但是win8及以下还是不行）
import os
import time
import sys
import re
from adb_shell.adb_device import AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

#连接设备用的RSA非对称秘钥
pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAivSgL4SgvC9U+TDvVO7C06QFZCmmdSb/aXAT6xGsUAODjrdTFmHCqwNyDKX5u8XTLeX7GWromjr9IVPOs4DQ/A5ECxXFfDcpFIy/lMuNm9tU2Qu5+eKBWsY4wmR6m8pzU3gQ8r4MEeifwQwJNANrXPWOimfibwTmCSsC7wAMlbj/OThbkVinXcBaxDZVvg7mEwKuyhmM99+MnXtK1uMcpLiVGBRKkzvsZYE9RVMtWLEsM8N5bEixBh8hBbF+yjnImsqyTtrH59UKWPzhV99C2dkGqER+2G1fcMJdu8JzxY7Td6OcwrOTThjIX3QXiu7A7oOj9rrgOAjHDJBWz852bwIDAQAB"
privkey = """-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCK9KAvhKC8L1T5
MO9U7sLTpAVkKaZ1Jv9pcBPrEaxQA4OOt1MWYcKrA3IMpfm7xdMt5fsZauiaOv0h
U86zgND8DkQLFcV8NykUjL+Uy42b21TZC7n54oFaxjjCZHqbynNTeBDyvgwR6J/B
DAk0A2tc9Y6KZ+JvBOYJKwLvAAyVuP85OFuRWKddwFrENlW+DuYTAq7KGYz334yd
e0rW4xykuJUYFEqTO+xlgT1FUy1YsSwzw3lsSLEGHyEFsX7KOciayrJO2sfn1QpY
/OFX30LZ2QaoRH7YbV9wwl27wnPFjtN3o5zCs5NOGMhfdBeK7sDug6P2uuA4CMcM
kFbPznZvAgMBAAECggEAVoG0t+B5sK5G2Fv+o4GBCQEz2geT+ZstpxZ/T/jQrX+C
h3bZ4zU5W/3pIraxopSlxA1JJanI5TuUGwtxw46OjFzVP5FuQMaWbJSzMI8Iud7y
E62ZOl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2cboMWrdxo6fjRs2cK
em2l8OC1Ejqj6oZaabPWFA==
-----END PRIVATE KEY-----
"""

#重启程序的子程序
def restart_program():
    print(' \n开始重新运行程序...\n ')
    os.system('cls')
    python = sys.executable
    os.execl(python, python, *sys.argv)

#字符进度条
def progress_bar(barname,time):  # (进度条头名称，进度时间)
  for i in range(1, 101):
    barname = barname+": {}%: "
    time = time/100
    print("\r", end="")
    print(barname.format(i), "▋" * (i // 1), end="")
    sys.stdout.flush()
    time.sleep(time)

#自定义字符进度条测试
def progress_bartest():
  os.system('cls')
  namef=input('firstname:')
  namel=input('lastname:')
  tim=input('time:')
  inf=input('fill:')
  info=input('f/s:')
  info=int(info)
  tim=int(tim)
  namef = namef+"  {}%: "
  namel = " "+namel
  for i in range(1, 101):
      print("\r", end="")
      print(namef.format(i), inf * (i // info), end=namel)
      sys.stdout.flush()
      time.sleep(tim)

def file_creation(rout,name,format,msg):  # (路径，文件名，后缀名，内容)
    full_path = rout + name + format
    file = open(full_path,'w')
    file.write(msg)
    file.close()

if __name__ == "__main__":
    # 转化获取的脚本目录为Python可用
    list = os.getcwd()
    list = list.replace("\\", "\\\\")
    list = list + "\\\\"
    res=input('此版本为canary版本，专门为测试使用，包含大量测试功能，极不稳定，请斟酌后使用\n此软件的任何版本皆免费（包括re版，高级版），不做也不允许做商业用途\n此项目由ReX 组织下ReX Wear 项目的 ReX iMoo Team 团队合力开发，版权及著作权属于ReX群组，请勿用其作商业用途，一经发现，将严肃处理！\nQQ群158015384，论坛watch.rexwe.net\n友链：https://ziantt.github.io/xtc-tool-web/\n')
    if res == '$$$':
        rese=input('')
        if rese == 'reset':
            che = os.path.isfile("start.mod")
            if che == True:
                os.system('del start.mod')
                os.system('cls')
    accept=input('是否同意上述条款(y/n):')
    if accept == 'y':
        che = os.path.isfile("start.mod")
        if che == True:
            file_object = open('start.mod', 'r')  # 文件对象
            try:
                output = file_object.read()  # 结果为str类型
            finally:
                file_object.close()
            print('测试网络连接中***')
            exit_code = os.system('ping www.gitee.com')  # 测试网络连接
            os.system('cls')
            if output == '1':
                # 连接小天才电话手表.
                print("正在连接手表...")
                os.system('cls')
                try:
                    device = AdbDeviceUsb()
                    device.connect(rsa_keys=[PythonRSASigner(pub=pubkey, priv=privkey)])
                except BaseException as err:
                    exceptionName = type(err).__name__
                    exceptionInfo = str(err)
                    if exceptionInfo == 'LIBUSB_ERROR_ACCESS [-3]':
                        file_creation(list, 'tips', '.vbs', 'MsgBox "正在尝试结束其他adb进程，若闪退可能为重复打开此脚本或无法结束adb.exe进程，请尝试自行打开任务管理器结束adb.exe进程后重新运行此脚本"')  # 创建提示文件
                        os.system('start tips.vbs')
                        os.system('del tips.vbs')
                        os.system('taskkill /T /F /IM adb.exe')
                        device = AdbDeviceUsb()
                        device.connect(rsa_keys=[PythonRSASigner(pub=pubkey, priv=privkey)])
                    elif exceptionInfo == 'No device available, or it is in the wrong configuration.':
                        print("您未连接设备，请连接后重新打开此软件")
                        input("按回车键退出.")
                        sys.exit(1)
                    else:
                        print("连接失败, 错误信息: \n%s: %s" % (exceptionName, exceptionInfo))
                        input("按回车键退出.")
                        sys.exit(1)

                print("连接成功")

                # 临时设置电池状态为不在充电
                device.shell('dumpsys battery unplug')

                os.system('cls')

                # 选择模式
                print(' \n请选择模式 \n1.批量安装手表/sdcard/目录下名称为1，2，3...的apk.\n2.安装手表中的apk(自定义路径).\n3.解锁push.\n4.安装web或本地的apk\n5.scrcpy投屏\n6.冻结系统及系统软件更新\n7.在QMMI安装软件\n8.修改分辨率，DPI\nback.强制返回桌面\nabout.关于')
                mode = input('请输入:')
                if mode == '1':
                    os.system('cls')
                    inputMsg = input(" \n请将软件放置于/sdcard/，改名为1.apk，2.apk.... \n并输入软件安装包数量:")
                    inputMsg = int(inputMsg)
                    for shit in range(inputMsg):
                        shit = str(shit + 1)

                        create = device.shell('pm install-create')
                        create = re.findall(r'\d+', create)
                        create = create[0]
                        file = ' install /sdcard/' + shit + '.apk'
                        device.shell('pm install-write ' + create + file)
                        device.shell('pm install-commit ' + create)

                if mode == '2':
                    os.system('cls')
                    while True:
                        inputMsg = input(" \n请输入软件的路径:")
                        create = device.shell('pm install-create')
                        create = re.findall(r'\d+', create)
                        create = create[0]
                        device.shell('pm install-write ' + create + inputMsg)
                        device.shell('pm install-commit ' + create)

                if mode == '3':
                    print("正在打开串口，将为您打开调试设置")
                    device.shell('am start com.xtc.selftest/.ui.ControllerActivity')
                    file_creation(list, 'tips', '.vbs',
                                  'MsgBox "请在手表上下拉页面，并勾选“打开串口”选项,并在回车后自动重启手表"')  # 创建提示文件
                    os.system('start tips.vbs')
                    os.system('del tips.vbs')
                    device.shell('reboot')
                    input('请您在手表重启完成后连接电脑，并重新打开程序')
                    sys.exit(1)

                if mode == '4':
                    inputMsg = input('请输入软件的路径或拖载:')
                    device.shell("rm /sdcard/apk.apk")
                    if inputMsg.startswith("http://") or inputMsg.startswith("https://") or inputMsg.startswith(
                            "ftp://"):
                        command = 'curl --output /sdcard/apk.apk ' + inputMsg
                        device.shell(command)

                    else:
                        if inputMsg.startswith('"') and inputMsg.endswith('"'):
                            inputMsg = inputMsg[1:-1]
                        if not os.path.isfile(inputMsg):
                            raise FileNotFoundError("此文件不存在.")
                        device.push(inputMsg, "/sdcard/apk.apk")

                    create = device.shell('pm install-create')
                    create = re.findall(r'\d+', create)
                    create = create[0]

                    device.shell('pm install-write ' + create + ' install /sdcard/apk.apk')
                    device.shell('pm install-commit ' + create)

                if mode == '5':
                    inf = os.system('scrcpy')
                    if inf == "'scrcpy' 不是内部或外部命令，也不是可运行的程序\n或批处理文件。":
                        os.system('cls')
                        input('请前往https://github.com/Genymobile/scrcpy下载后放入此脚本同目录下，或配置环境变量后再运行此模式')
                        sys.exit(1)
                    else:
                        os.system('cls')
                        input('投屏软件来自github开源项目，不可与此软件同时使用')
                        os.system('start scrcpy.exe')
                        sys.exit(1)

                if mode == '6':
                    device.shell('pm disable-user com.xtc.appupdate')
                    device.shell('pm disable-user com.xtc.systemupdate_i11')
                    sys.exit(1)

                if mode == '7':
                    check = device.shell('pm list packages com.qualcomm.qti.qmmi')
                    check = check[0:-1]
                    if check == 'package:com.qualcomm.qti.qmmi':
                        input('回车后自动进入手表QMMI')
                        device.shell('am start com.qualcomm.qti.qmmi/.framework.MainActivity')
                        os.system('cls')
                        input('请依次点击重启，选择重启到QMMI，并确认，完成后请回车')
                        os.system('cls')
                        progress_bar("reboot", 70)
                        os.system('cls')
                        while True:
                            inputMsg = input('请输入软件的路径或拖载:')
                            if inputMsg == 'back':
                                device.shell('adb shell input keyevent KEYCODE_HOME')
                                sys.exit(1)

                            device.shell("rm /sdcard/apk.apk")
                            if inputMsg.startswith("http://") or inputMsg.startswith("https://") or inputMsg.startswith(
                                    "ftp://"):
                                command = 'curl --output /sdcard/apk.apk ' + inputMsg
                                device.shell(command)

                            else:
                                if inputMsg.startswith('"') and inputMsg.endswith('"'):
                                    inputMsg = inputMsg[1:-1]
                                if not os.path.isfile(inputMsg):
                                    raise FileNotFoundError("此文件不存在.")
                                device.push(inputMsg, "/sdcard/apk.apk")

                            create = device.shell('pm install-create')
                            create = re.findall(r'\d+', create)
                            create = create[0]

                            device.shell('pm install-write ' + create + ' install /sdcard/apk.apk')
                            device.shell('pm install-commit ' + create)
                            os.system('cls')
                            print('若要返回正常启动，请在输入软件路径位置输入back即可')
                        # 正常启动手表 device.shell('adb shell input keyevent KEYCODE_HOME')
                    else:
                        os.system('cls')
                        print('您的手表未安装QMMI，请安装后重新运行此模式')
                        sys.exit(1)

                if mode == '8':
                    print('1.查看分辨率\n2.修改分辨率\n3.查看DPI\n4.修改DPI\n5.恢复原始分辨率\n6.恢复原始DPI')
                    moded = input('选择模式:')
                    os.system('cls')
                    if moded == '1':
                        out = device.shell('wm size')
                        input(out)

                    if moded == '2':
                        out = device.shell('wm size')
                        out = '当前分辨率' + out
                        print(out)
                        infot = input('横向分辨率修改为:')
                        infol = input('纵向分辨率修改为:')
                        info = infot + 'x' + infol
                        info = 'wm size ' + info
                        device.shell(info)
                        out = device.shell('wm size')
                        input(out)

                    if moded == '3':
                        out = device.shell('wm density')
                        input(out)

                    if moded == '4':
                        out = device.shell('wm density')
                        out = '当前DPI' + out
                        print(out)
                        info = input('DPI修改为：')
                        info = 'wm density ' + info
                        device.shell(info)
                        out = device.shell('wm density')
                        input(out)

                    if moded == '5':
                        device.shell('wm size reset')
                        out = device.shell('wm size')
                        out = '恢复完成，当前分辨率' + out
                        input(out)

                    if moded == '6':
                        device.shell('wm density reset')
                        out = device.shell('wm density')
                        out = '恢复完成，当前DPI' + out
                        input(out)

                    else:
                        print('没有此选项')
                        os.system('cls')

                if mode == 'back':
                    os.system('cls')
                    device.shell('input keyevent KEYCODE_HOME')
                    sys.exit(1)

                if mode == 'bartest':
                    progress_bartest()
                    sys.exit(1)

                if mode == 'about':
                    os.system('cls')
                    input('此项目由ReX 组织下ReX Wear 项目的 ReX iMoo Team 团队合力开发，版权及著作权属于ReX群组，请勿用其作商业用途，一经发现，将严肃处理！\nQQ群158015384，论坛watch.rexwe.net')
                    sys.exit(1)

                if mode == '114514':
                    os.system('cls')
                    input('药不能停！')
                    sys.exit(1)

                if mode == 'Re':
                    os.system('cls')
                    input('阔耐的Re诶，归我啦！')
                    sys.exit(1)

                else:
                    print('没有此选项')
                    os.system('cls')

            if output == '2':
                os.system('cls')
                os.system('start https://ziantt.github.io/xtc-tool-web/')
                sys.exit(1)

        else:
            print('启动本软件或网页版XTC Tool\n1.启动本脚本\n2.打开网页版XTC Tool')
            ou = input('选择默认启动模式：')
            if ou == '1':
                file_creation(list, 'start', '.mod', '1')  # 创建数据文件1
                print('设置完成，尝试重启程序')
                restart_program()

            if ou == '2':
                file_creation(list, 'start', '.mod', '2')  # 创建数据文件2
                print('设置完成，尝试重启程序')
                restart_program()

        if ou == '2':
            if exit_code == 0:  # 网络连接成功
                print('服务器连接成功')
                os.system('cls')
                os.system('start https://ziantt.github.io/xtc-tool-web/')
                sys.exit(1)

            else:
                input('服务器连接失败，无法访问网页版Tool')
                sys.exit(1)


        else:
            print('没有此选项')
            os.system('cls')

    else:
        sys.exit(1)
